/*
 * ExprType.java
 *
 * Created on July 31, 2007, 7:54 PM
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.variable.types;

import tal.drivechain.chain.ChainNode;
import tal.drivechain.matchers.ValueMatcher;
import tal.drivechain.variable.Variable;
import tal.drivechain.variable.expressions.CustomExpression;

/**
 * A DataType class that stores expressions.
 * Name: "expr"
 * Value object: {@link CustomExpression}
 * Type code: 3
 * Syntax: expr:(<return type>)<expression>
 * Can't cast from any other data type. Evaluates the expression
 * When extractValue is called.
 *
 * @author Tal Eisenberg
 */
public final class ExprType extends DataType {
    /** Creates a new instance of ExprType */
    public ExprType() {
        super("expr", CustomExpression.class, 3);
    }

    @Override
    public boolean isValue(String def) {
        try {
            this.parseValue(def);
            return true;
        } catch (IllegalArgumentException ie) {
            //find out if the cause is an error in SetProperty
            for (StackTraceElement s : ie.getStackTrace()) {
                if (s.getClassName().indexOf(".expressions.")!=-1)
                    return true;
            }
            return false;
        }
    }

    public Object parseValue(String value) throws IllegalArgumentException {
        DataType exprReturn;
        if (value.charAt(0)=='[' && value.indexOf(']')!=-1) {
            exprReturn = DataType.parseType(value.substring(1,value.indexOf(']')));
        } else throw new IllegalArgumentException("Missing return data type for expr: " + value);
        return exprReturn.createExpression(value.substring(value.indexOf(']')+1));
    }

    @Override
    public Object extractValue(Object value, ChainNode context) {
        return ((CustomExpression)value).evaluate(context);
    }

    @Override
    public Variable evaluateVar(Variable var, ChainNode context) {
        return ((CustomExpression)var.getInternalValue()).evaluateAtHome(context, this.getName());
    }
    
    @Override
    public CustomExpression createExpression(String value) throws IllegalArgumentException {
        throw new IllegalArgumentException("An expr variable can't be assigned to a expression.");
    }

    @Override
    public ValueMatcher createValueMatcher() throws IllegalArgumentException {
        throw new IllegalArgumentException("Value matchers are not supported for expr variables.");
    }
}
